﻿using System;
using System.Collections;
using System.Collections.Generic;
using Game.ui;
using GameFramework.common;
using GameFramework.events;
using GameFramework.timer;
using GameFramework.UI;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace GameFramework.scene
{
    public class SceneLoadingManager:MonoSingleton<SceneLoadingManager>
    {
        private bool isLoading = false;

        /// <summary>
        /// 切换场景，需要显示加载UI
        /// </summary>
        public void LoadingSceneWithLoadingUI(string sceneName,Action loadComplete = null)
        {
            string activeScene = SceneManager.GetActiveScene().name;
            if (activeScene.Equals(sceneName))
            {
                return;
            }
            //正在加载场景中..
            if (isLoading)
            {
                return;
            }

            //显示场景加载UI
            // UIManager.Instance.showLoading();
            isLoading = true;
            AsyncOperation loadSceneAsync = SceneManager.LoadSceneAsync(sceneName);
            loadSceneAsync.allowSceneActivation = false;
            UIManager.Instance.show<SceneLoadingView>();
            StartCoroutine(loadScene(sceneName,loadSceneAsync,loadComplete));
        }
        /// <summary>
        /// 切换场景，需要显示加载UI
        /// </summary>
        public void LoadingSceneSync(string sceneName,Action loadComplete = null)
        {
            string activeScene = SceneManager.GetActiveScene().name;
            if (activeScene.Equals(sceneName))
            {
                return;
            }
            //正在加载场景中..
            if (isLoading)
            {
                return;
            }

            //显示场景加载UI
            // UIManager.Instance.showLoading();
            isLoading = true;
            AsyncOperation loadSceneAsync = SceneManager.LoadSceneAsync(sceneName);
            loadSceneAsync.allowSceneActivation = false;
            
            float goneTime = 0;
            MVPEventManager.Instance.SceneLoadingView_ProcessChange?.Invoke(0);
            //等待资源加载完毕
            while (loadSceneAsync.progress < 0.9f)
            {
                //发布加载进度的事件
                goneTime += Time.deltaTime;
                MVPEventManager.Instance.SceneLoadingView_ProcessChange?.Invoke(loadSceneAsync.progress);
            }

            if (goneTime < 2)
            {
                TimerManager.Instance.schedule(timer =>
                {
                    loadingDone(loadSceneAsync, loadComplete);
                }, 2 - goneTime);
            }
            else
            {
                loadingDone(loadSceneAsync, loadComplete);
            }
        }

        private IEnumerator loadScene(string scneneName,AsyncOperation asyncOperation, Action loadComplete)
        {
            float goneTime = 0;
            MVPEventManager.Instance.SceneLoadingView_ProcessChange?.Invoke(0);
            //等待资源加载完毕
            while (asyncOperation.progress < 0.9f)
            {
                //发布加载进度的事件
                goneTime += Time.deltaTime;
                MVPEventManager.Instance.SceneLoadingView_ProcessChange?.Invoke(asyncOperation.progress);
                yield return null;
            }

            if (goneTime < 2)
            {
                TimerManager.Instance.schedule(timer =>
                {
                    loadingDone(asyncOperation, loadComplete);
                }, 2 - goneTime);
            }
            else
            {
                loadingDone(asyncOperation, loadComplete);
            }
            

        }

        private void loadingDone(AsyncOperation asyncOperation,Action loadComplete)
        {
            //当asyncOperation.allowSceneActivation 为false，则asyncOperation.Progress最多只能到达0.9
            //我们人为把它们凑成1，可以方便外部进度条的显示
            MVPEventManager.Instance.SceneLoadingView_ProcessChange?.Invoke(1);
 
            asyncOperation.allowSceneActivation = true;
 
            //加载资源完毕后执行的逻辑
            try
            {
                loadComplete?.Invoke();
            }
            catch (Exception e)
            {
                Debug.LogError(e);
            }

            isLoading = false;
        }

    }
}